#ifndef ZIGBEEWIDGET_H
#define ZIGBEEWIDGET_H

//#include <qstring>
//#include <QDialog>
//#include <QTime>
//#include <QPushButton>
//#include <QCloseEvent>
//#include <qlcdnumber.h>

#include <QWidget>
#include <QThread>
#include "baseType.h"

namespace Ui {
class Widget;
}

class MessageQueue;
class WorkThread;

class Widget : public QWidget
{
    Q_OBJECT
    
public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private slots:
    void on_ConnectButton_clicked();

    void on_ReadButton_clicked();

    void on_HelpButton_clicked();

    void on_WriteButton_clicked();

    void on_ResetButton_clicked();

    void on_WriteNodeType_currentIndexChanged(int index);

    void on_WriteChannel_currentIndexChanged(int index);

    void on_WritePinIdOne_editingFinished();

    void on_WritePinIdTwo_editingFinished();

    void on_WriteAddrOne_editingFinished();

    void on_WriteAddrTwo_editingFinished();

    void on_RouterAddrOne_editingFinished();

    void on_RouterAddrTwo_editingFinished();

private:
    bool checkNumCorrect(QString&);

private:

    friend class WorkThread;
    Ui::Widget *ui;
    MessageQueue *mMessageQueue;
    WorkThread *mWorkThread;

};

/******************定义一个条件变量控制线程运行*************************/

class MessageQueue {
public:
    typedef enum {
        MESSAGE_CONNECT,
        MESSAGE_RESET,
        MESSAGE_WRITE_ARG,
        MESSAGE_READ_INFO,
        MESSAGE_EXIT,
        MESSAGE_UNKNOW
    }MessageType;

    MessageQueue(Widget*);
    ~MessageQueue();

    _S32 sendMessage(MessageType type);
    MessageType waitMessage();

    bool messageIsBusy();
private:
    Widget *mWidget;

    MessageType mMessageType;
    pthread_mutex_t condMutex;  //此锁用于条件变量加减锁
    pthread_cond_t  mCond;      //此条件变量用于何时运行
};

/******************定义一个线程，耗时工作由他来做*************************/
class WorkThread : public QThread {
    Q_OBJECT
    public:

        explicit WorkThread(Widget*);
        ~WorkThread();

        void startWork();

        void run();
    signals:

    public slots:

    private:
        Widget *mWidget;
        bool running;

        WorkThread& operator =(WorkThread&);
};

#endif // ZIGBEEWIDGET_H
